<template>
  <div class="goodinfo-container">
    <!-- 小球 -->

    <transition
      @before-enter="beforeEnter"
      @enter="enter"
      @after-enter="afterEnter"
    >
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品图片区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <carouses :lunbotuList="lunboList" :isfull="false"></carouses>
        </div>
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsInfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：<del>￥{{ goodsInfo.market_price }}</del> &nbsp
            销售价：<span>￥{{ goodsInfo.sell_price }}</span>
          </p>
          <!-- 设置添加的数量 和 库存的最大值 -->
          <numbox @getCount="getAccount" :max="goodsInfo.stock_quantity">
          </numbox>

          <div class="button">
            <el-button type="primary" size="small">立即购买</el-button>
            <el-button type="danger" size="small" @click="addShopCar"
              >加入购物车</el-button
            >
          </div>
        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{ goodsInfo.goods_no }}</p>
          <p>库存情况：{{ goodsInfo.stock_quantity }}</p>
          <p>上架时间：{{ goodsInfo.add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <el-button
          type="primary"
          plain
          style="width: 100%; margin-bottom: 10px"
          @click="goDesc(id)"
          >图文介绍</el-button
        >
        <el-button
          type="danger"
          plain
          style="width: 100%; margin-left: 0"
          @click="goComment(id)"
          >商品评论</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import carouses from "../subcomponents/carousel.vue";

import numbox from "../subcomponents/goodsNumBox.vue";

export default {
  data() {
    return {
      lunboList: [],
      id: this.$route.params.id,
      goodsInfo: {},
      ballFlag: false,
      goodsAccount: 1,
    };
  },

  created() {
    this.getLunbo(), this.getGoodsInfo();
  },

  methods: {
    getLunbo() {
      this.$http.get("api/getthumimages/" + this.id).then((result) => {
        if (result.body.status === 0) {
          result.body.message.forEach((item) => {
            item.img = item.src;
          });

          this.lunboList = result.body.message;
        }
      });
    },

    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then((result) => {
        if (result.body.status === 0) {
          this.goodsInfo = result.body.message[0];
        }
      });
    },

    goDesc(id) {
      this.$router.push({ name: "goodsdesc", params: { id } });
    },

    goComment(id) {
      this.$router.push({ name: "goodscomment", params: { id } });
    },

    addShopCar() {
      this.ballFlag = !this.ballFlag; //购物小球的状态

      //定义一个对象
      var goodsinfo = {
        id: this.id,
        count: this.goodsAccount,
        selectStatus: true,      
        price: this.goodsInfo.sell_price,

      };

      this.$store.commit("getToCar", goodsinfo)

     

    },

    beforeEnter(el) {
      el.style.transform = "translate(0 , 0)";
    },

    enter(el, done) {
      //优化小球在不同分辨率和滚动后能精准落位
      //1.使用getBoundingClientRect()方法，有top left bottom right 四个返回值
      //2.先获取badge 和 ball相应的属性
      //3.由购物车badge.top - ball.top  badge.left - ball.left 获取准确位置

      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      const ballPosition = this.$refs.ball.getBoundingClientRect();

      const moveX = badgePosition.left - ballPosition.left;

      const moveY = badgePosition.top - ballPosition.top;

      el.offsetWidth;
      el.style.transform = `translate(${moveX}px , ${moveY}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },

    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },

    getAccount(count) {
      this.goodsAccount = count;

      console.log(this.goodsAccount);
    },
  },

  components: {
    carouses,
    numbox,
  },
};
</script>

<style lang="scss" scoped>
.goodinfo-container {
  background-color: gainsboro;
  overflow: hidden;

  .price {
    font-size: 12px;

    span {
      color: lightcoral;
      font-weight: bold;
      font-size: 14px;
    }
  }

  .mui-card-footer {
    display: block;
  }

  .ball {
    width: 15px;
    height: 15px;
    border-radius: 10px;
    background-color: red;
    position: absolute;
    z-index: 99;
    left: 84px;
    top: 438px;
  }
}
</style>